﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.IO;
using System.Threading;
using FileWatcher;
using EmailSendWizard;


namespace WindowsSevicer
{
    public class HKEXMonitor

    {
        public HKEXMonitor()
        {
            DailyCaptureOn hsifCapture = new DailyCaptureOn("hsif");
            Thread hsif = new Thread(hsifCapture.threadStart);
            hsif.Start();
            DailyCaptureOn hsioCapture = new DailyCaptureOn("hsio");
            Thread hsio = new Thread(hsioCapture.threadStart);
            hsif.Start();
            DailyCaptureOn hhifCapture = new DailyCaptureOn("hhif");
            Thread hhif = new Thread(hhifCapture.threadStart);
            hsif.Start();
            DailyCaptureOn dqeCapture = new DailyCaptureOn("dqe");
            Thread dqe = new Thread(dqeCapture.threadStart);
            hsif.Start();
    }
        //Workflow: DailyCaptureOn   <-------------------|                
        //->FileDownload(if exist)                       |
        //   ->UnzipAndDeleted(work with the zip file)   |
        //->DataFileMonitor(work with the csv file)      |
        //   ->InsertToDataBase(and delete the CSV file) |
        //->SendAnEmail         
        public class DailyCaptureOn
        {

            private string typeOfstock;

            public DailyCaptureOn(string typeOfstock)
            {
                // TODO: Complete member initialization
                this.typeOfstock = typeOfstock;
            }

            private string result;

            public void threadStart()
            {
                Thread.Sleep(1000 * 3600);
                //Implement capture the HKEX daily report website each hour
                //to get the daily report.
                FileDownload fDownload = new FileDownload();
                DateTime dateTime = DateTime.Now;
                string dateyymmdd = dateTime.ToString("yymmdd");
                string url = "http://www.hkex.com.hk/eng/stat/dmstat/dayrpt/" + typeOfstock + dateyymmdd + ".zip";
                string result = fDownload.Download(Properties.Settings.Default.BufferDirectory, url);
                DataFileMonitor monitor = new DataFileMonitor();
                monitor.watch(Properties.Settings.Default.BufferDirectory, "*.csv");
                if (result == "downloaded success")
                {
                    StoreTheData(Properties.Settings.Default.BufferDirectory, Path.GetFileName(url));
                    
                }
                else
                {
                    threadStart();
                }
            }

            private void StoreTheData(string dataDirectory, string fileName)
            {
                //Start to decompose the file
                UnzipAndDeleted unzipDelete = new UnzipAndDeleted();
                unzipDelete.unzipTo(dataDirectory);
            }

            public void SendAnEmail()
            {
                EmailSentAction sent = new EmailSentAction("smtp.gmail.com", "autofinacedatacapture@gmail.com", "Assignment", 587, true);
                string dateString = DateTime.Now.ToShortDateString();
                sent.SendMail("AutoFinaceDataCapture@gmail.com", Properties.Settings.Default.SettingEmail, "Report of " + dateString + " had released.", result);
            }
        }
    }
}
